/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001-2003, Ximian, Inc.
 * Copyright (C) 2013 Igalia, S.L.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#include <libsoup/soup.h>

static SoupSession *session;
static GMainLoop *loop = NULL;

static void
finished (SoupSession *session, SoupMessage *msg, gpointer loop)
{
        printf("fininsh\n");
	g_main_loop_quit (loop);
}

static void
get_url (const char *url)
{
	const char *name;
	SoupMessage *msg;
	FILE *output_file = NULL;

        session = g_object_new (SOUP_TYPE_SESSION,
                                SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_DECODER,
                                SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_COOKIE_JAR,
                                SOUP_SESSION_USER_AGENT, "get ",
                                SOUP_SESSION_ACCEPT_LANGUAGE_AUTO, TRUE,
                                NULL);

	msg = soup_message_new ("POST", url);
        //xiecc delete
	//soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT);
        char *body = "{\"api\":\"http://localhost:1985/rtc/v1/publish/\", \"streamurl\":\"webrtc://localhost/live/livestream\",\
                     \"sdp\": \"v=0\r\no=- 3484854617540489897 0 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=ice-options:trickle\r\na=group:BUNDLE video0 audio1 \r\nm=video 9 UDP/TLS/RTP/SAVPF 125\r\nc=IN IP4 0.0.0.0\r\na=setup:actpass\r\na=ice-ufrag:O6i8yCCK8JFZ+Xfea8Dit3zokD0PRo6f\r\na=ice-pwd:eMQboQ4zhKoHXMac1eqixdxOk4Xbd76A\r\na=rtcp-mux\r\na=rtcp-rsize\r\na=sendrecv\r\na=rtpmap:125 H264/90000\r\na=rtcp-fb:125 nack pli\r\na=framerate:30\r\na=ssrc:3074290200 msid:user213429320@host-47b9719 webrtctransceiver0\r\na=ssrc:3074290200 cname:user213429320@host-47b9719\r\na=mid:video0\r\na=fingerprint:sha-256 51:89:1D:0C:14:73:B7:31:79:99:33:B7:9F:AB:A3:E7:48:2A:5C:28:49:DF:12:59:BD:23:67:3D:79:AF:ED:86\r\nm=audio 0 UDP/TLS/RTP/SAVPF 97\r\nc=IN IP4 0.0.0.0\r\na=setup:actpass\r\na=ice-ufrag:O6i8yCCK8JFZ+Xfea8Dit3zokD0PRo6f\r\na=ice-pwd:eMQboQ4zhKoHXMac1eqixdxOk4Xbd76A\r\na=bundle-only\r\na=rtcp-mux\r\na=rtcp-rsize\r\na=sendrecv\r\na=rtpmap:97 OPUS/48000/2\r\na=rtcp-fb:97 nack pli\r\na=fmtp:97 sprop-maxcapturerate=48000;sprop-stereo=0\r\na=ssrc:2738609400 msid:user213429320@host-47b9719 webrtctransceiver1\r\na=ssrc:2738609400 cname:user213429320@host-47b9719\r\na=mid:audio1\r\na=fingerprint:sha-256 51:89:1D:0C:14:73:B7:31:79:99:33:B7:9F:AB:A3:E7:48:2A:5C:28:49:DF:12:59:BD:23:67:3D:79:AF:ED:86\r\n\"}";

        soup_message_headers_append (msg->request_headers,  "Content-Type", "application/json");

        soup_message_body_append (msg->request_body, SOUP_MEMORY_TEMPORARY, body, strlen(body));

	if (loop) {
                printf("async\n");
		g_object_ref (msg);
		soup_session_queue_message (session, msg, finished, loop);
		g_main_loop_run (loop);
	} else
		soup_session_send_message (session, msg);

	name = soup_message_get_uri (msg)->path;

	if (SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
	} else if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
		output_file = stdout;
		if (output_file) {
			fwrite (msg->response_body->data,
				1,
				msg->response_body->length,
				output_file);
		}
	}
	g_object_unref (msg);
	g_object_unref (session);
}


int main (int argc, char **argv)
{
        gboolean synchronous  = 0;
	const char *url;
	SoupURI *parsed;

	url = argv[1];
        if (url == NULL){
           url = "http://localhost:1985/rtc/v1/publish/";
        }
	parsed = soup_uri_new (url);
	if (!parsed) {
		g_printerr ("Could not parse '%s' as a URL\n", url);
		exit (1);
	}
	soup_uri_free (parsed);
	if (!synchronous)
	    loop = g_main_loop_new (NULL, TRUE);
	get_url(url);
	if (!synchronous)
	   g_main_loop_unref (loop);
	return 0;
}
